﻿using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using FlashlightRecorder.Repositories;
using System.Threading.Tasks;
using MVVMCommon.ViewModels.Commands;
using FlashlightRecorder.ViewModels;
using System.Linq;

namespace FlashlightRecorder.ViewModels
{
    public class MainViewModel : DisposableHost, INotifyPropertyChanged
    {

        SimpleRepository<ViewModels.Record> _recordRepository = null;
        public ReactiveCommand PlayClipCommand { get; private set; }
        public ReactiveCommand DeleteClipCommand { get; private set; }
        public MainViewModel()
        {
            PlayClipCommand = new ReactiveCommand(true);
            PlayClipCommand.Subscribe
                (
                    p =>
                    {
                        var item = p as Record;
                        if (item != null)
                        {
                            this.PlayerModel.CurrentRecord = item;
                            this.PageIndex = 2;
                        }

                    }
                )
                .RegisterDispose(this);
            DeleteClipCommand = new ReactiveCommand(true);
            DeleteClipCommand
                .Subscribe(
                  async  p =>
                    {
                        var msg = MessageBox.Show("Are you sure to delete this clip?", "Warning", MessageBoxButton.OKCancel);
                        if (msg == MessageBoxResult.OK)
                        {


                            var item = p as Record;
                            if (item != null)
                            {
                                this.Records.Where(x => x.Name == item.Name)
                                    .ToList()
                                    .ForEach(x => Records.Remove(x));
                                await this._recordRepository.RemoveAsync(item.Name);
                            }
                        }
                        else
                        { 
                        }
                    }
                )
               .RegisterDispose(this);


            this.Records = new ObservableCollection<ViewModels.Record>();

            PlayerModel = new ViewModels.Player_Model();
            RecorderModel = new ViewModels.Recorder_Model() {  Root=this};
            ColorModel = new Color_Model();
            var s = SimpleRepository<ViewModels.Record>.CreateSimpleRepositoryAsync("Default").Result;
            this._recordRepository = s;
            this.PlayerModel.RecordRepository = s;
            this.RecorderModel.RecordRepository = s;
            this.IsDataLoaded = true;
            LoadItemsAsync();
        }



        public async Task LoadItemsAsync()
        {
            this.Records.Clear();
            foreach (var item in await _recordRepository.GetAllAsync())
            {
                item.Value.Root = this;
                this.Records.Add(item.Value);
            }


        }

        ViewModels.Player_Model _playerModel;

        ViewModels.Color_Model _colorModel;

        public ViewModels.Color_Model ColorModel
        {
            get { return _colorModel; }
            set { _colorModel = value; NotifyPropertyChanged("ColorModel"); }
        }

        public ViewModels.Player_Model PlayerModel
        {
            get { return _playerModel; }
            set { _playerModel = value; NotifyPropertyChanged("PlayerModel"); }
        }
        ViewModels.Recorder_Model _recorderModel;

        public ViewModels.Recorder_Model RecorderModel
        {
            get { return _recorderModel; }
            set { _recorderModel = value; NotifyPropertyChanged("RecorderModel"); }
        }

        int _pageIndex;
        public int PageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; NotifyPropertyChanged("PageIndex"); }
        }



        ObservableCollection<ViewModels.Record> _records;

        public ObservableCollection<ViewModels.Record> Records
        {
            get { return _records; }
            set { _records = value; NotifyPropertyChanged("Records"); }
        }


        private string _sampleProperty = "Sample Runtime Property Value";
        /// <summary>   
        /// Sample ViewModel property; this property is used in the view to display its value using a Binding
        /// </summary>
        /// <returns></returns>
        public string SampleProperty
        {
            get
            {
                return _sampleProperty;
            }
            set
            {
                if (value != _sampleProperty)
                {
                    _sampleProperty = value;
                    NotifyPropertyChanged("SampleProperty");
                }
            }
        }

        public bool IsDataLoaded
        {
            get;
            private set;
        }



        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}